package org.csu.petserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.csu.petserver.domain.Inventory;
import org.csu.petserver.domain.Item;
import org.csu.petserver.persistence.InventoryMapper;
import org.csu.petserver.service.InventoryInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("InventoryInfo")
@MapperScan("org.csu.petserver.persistence")
public class InventoryInfoImpl implements InventoryInfo {
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private Inventory inventory;
    @Override
    public Integer getQuantityOfItem(Item item) {
        Inventory inventory = inventoryMapper.selectById(item.getId());
        return inventory.getQuantity();
    }

    @Override
    public boolean updateQuantity(Item item, Integer quantity) {
        inventory.setId(item.getId());
        inventory.setQuantity(quantity);
        inventoryMapper.updateById(inventory);
        return true;
    }

    @Override
    public boolean increaseOne(Item item) {
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",item.getId());
        Inventory inventory = inventoryMapper.selectOne(queryWrapper);
        inventory.setQuantity(inventory.getQuantity()+1);
        inventoryMapper.updateById(inventory);
        return true;
    }

    @Override
    public boolean decreaseOne(Item item) {
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",item.getId());
        Inventory inventory = inventoryMapper.selectOne(queryWrapper);
        inventory.setQuantity(inventory.getQuantity()-1);
        inventoryMapper.updateById(inventory);
        return true;
    }
}
